package com.springboot.springbootredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: TongRui乀
 * @Date: 2019/9/20 11:10
 * @description：
 */
//@Component
public class RedisTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(fixedDelay = 1000 * 60 * 11)
    public void test(){

        String value = (String) this.redisTemplate.opsForValue().get("1");

        System.err.println(value);

        LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) this.redisTemplate.getConnectionFactory();

        connectionFactory.setDatabase(2);

        connectionFactory.resetConnection();

        redisTemplate.setConnectionFactory(connectionFactory);

        redisTemplate.opsForValue().set("2",2);

        redisTemplate.expire("2", 5, TimeUnit.SECONDS);

        System.err.println(redisTemplate.opsForValue().get("2"));
    }

}
